﻿using Architecture.Repository.Interface;
using Architecture.Repository.Interface.Traceability;
using Traceability.Domain.TraceabilityDto;

namespace Traceability.Domain.Service.EnterpriseServices
{
    /// <summary>
    /// 企业服务
    /// </summary>
    public class EnterpriseService : IEnterpriseService
    {
        /// <summary>
        /// 企业服务
        /// </summary>
        protected readonly IEnterpriseRepository enterpriseRepository;

        /// <summary>
        /// 图片仓储
        /// </summary>
        protected readonly IPictureRepository pictureRepository;

        /// <summary>
        /// 省信息仓储
        /// </summary>
        protected readonly IProvinceRepository provinceRepository;

        /// <summary>
        /// 市信息仓储
        /// </summary>
        protected readonly ICityRepository cityRepository;

        /// <summary>
        /// 县信息仓储
        /// </summary>
        protected readonly ICountryRepository courierRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="enterpriseRepository">企业服务</param>
        /// <param name="pictureRepository">图片仓储</param>
        /// <param name="provinceRepository">省信息仓储</param>
        /// <param name="cityRepository">市信息仓储</param>
        /// <param name="courierRepository">县信息仓储</param>
        public EnterpriseService(IEnterpriseRepository enterpriseRepository, IPictureRepository pictureRepository, IProvinceRepository provinceRepository, ICityRepository cityRepository, ICountryRepository courierRepository)
        {
            this.enterpriseRepository = enterpriseRepository;
            this.pictureRepository = pictureRepository;
            this.provinceRepository = provinceRepository;
            this.cityRepository = cityRepository;
            this.courierRepository = courierRepository;
        }

        /// <summary>
        /// 添加养殖信息
        /// </summary>
        /// <param name="shoppingId"></param>
        /// <param name="enterprise"></param>
        /// <returns></returns>
        public async Task<int> AddEnterpriseBreed(int shoppingId, Enterprise enterprise)
        {
            var res = await enterpriseRepository.GetAsync(x => x.ShoppingId == shoppingId);
            res.FirstOrDefault().livestockFarm = enterprise.livestockFarm;
            res.FirstOrDefault().PersonInCharge = enterprise.PersonInCharge;
            res.FirstOrDefault().InventoryQuantity = enterprise.InventoryQuantity;
            res.FirstOrDefault().CreatedBy= enterprise.CreatedBy;
            res.FirstOrDefault().CreatedDate = DateTime.Now;
            return await enterpriseRepository.UpdateAsync(res.FirstOrDefault());
        }

        /// <summary>
        /// 添加屠宰信息
        /// </summary>
        /// <param name="shoppingId"></param>
        /// <param name="enterprise"></param>
        /// <returns></returns>
        public async Task<int> AddEnterpriseSlaughter(int shoppingId, Enterprise enterprise)
        {
            var res = await enterpriseRepository.GetAsync(x => x.ShoppingId == shoppingId);
            res.FirstOrDefault().SlaughterCompanyName = enterprise.SlaughterCompanyName;
            res.FirstOrDefault().EstablishmentTime = enterprise.EstablishmentTime;
            res.FirstOrDefault().CompanyProfile = enterprise.CompanyProfile;
            res.FirstOrDefault().ProvinceId = enterprise.ProvinceId;
            res.FirstOrDefault().CityId = enterprise.CityId;
            res.FirstOrDefault().CountyId = enterprise.CountyId;
            res.FirstOrDefault().UpdatedBy = enterprise.UpdatedBy;
            res.FirstOrDefault().UpdatedDate = DateTime.Now;
            return await enterpriseRepository.UpdateAsync(res.FirstOrDefault());
        }


        /// <summary>
        /// 添加深加工信息
        /// </summary>
        /// <param name="shoppingId"></param>
        /// <param name="enterprise"></param>
        /// <returns></returns>
        public async Task<int> AddEnterpriseDeepProcessing(int shoppingId, Enterprise enterprise)
        {
            var res = await enterpriseRepository.GetAsync(x => x.ShoppingId == shoppingId);
            res.FirstOrDefault().DeepProcessingCompanyName = enterprise.DeepProcessingCompanyName;
            res.FirstOrDefault().ProductionLicenseNumber = enterprise.ProductionLicenseNumber;
            res.FirstOrDefault().UpdatedBy = enterprise.UpdatedBy;
            res.FirstOrDefault().UpdatedDate = DateTime.Now;
            return await enterpriseRepository.UpdateAsync(res.FirstOrDefault());
        }

        /// <summary>
        /// 获取企业列表
        /// </summary>
        /// <returns>返回企业信息列表</returns>
        public async Task<List<EnterpriseDto>> GetEnterpriseList()
        {
            var list = (from a in await enterpriseRepository.GetAllAsync()
                       join b in await pictureRepository.GetAllAsync()
                       on a.EnterpriseId equals b.EnterpriseId
                       join c in await provinceRepository.GetAllAsync()
                       on a.ProvinceId equals c.ProvinceId
                       join d in await cityRepository.GetAllAsync()
                       on a.CityId equals d.CityId
                       join e in await courierRepository.GetAllAsync()
                       on a.CountyId equals e.CountyId
                       where !a.IsDeleted && !b.IsDeleted
                       select new EnterpriseDto
                       {
                           EnterpriseId = a.EnterpriseId,
                           livestockFarm = a.livestockFarm,
                           PersonInCharge = a.PersonInCharge,
                           InventoryQuantity = a.InventoryQuantity,
                           SlaughterCompanyName = a.SlaughterCompanyName,
                           EstablishmentTime = a.EstablishmentTime,
                           CompanyProfile = a.CompanyProfile,
                           ProvinceId = a.ProvinceId,
                           CityId = a.CityId,
                           CountyId = a.CountyId,
                           DeepProcessingCompanyName = a.DeepProcessingCompanyName,
                           ProductionLicenseNumber = a.ProductionLicenseNumber,
                           ShoppingId = a.ShoppingId,
                           CreatedBy = a.CreatedBy,
                           CreatedDate = a.CreatedDate,
                           UpdatedBy = a.UpdatedBy,
                           UpdatedDate = a.UpdatedDate,
                           DeletedBy = a.DeletedBy,
                           DeletedDate = a.DeletedDate,
                           IsDeleted = a.IsDeleted,
                           PictureId = b.PictureId,
                           PictureName = b.PictureName,
                           ProvinceName=c.ProvinceName,
                           CountyName=d.CityName,
                           CityName=d.CityName,
                           
                       }).ToList();
            return list;
        }
    }
}
